package cn.net.mugui.net.pc.xlxs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Consumer;

import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.internal.win32.OPENFILENAME;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.internal.win32.TCHAR;

import com.mugui.Dui.DButton;
import com.mugui.Dui.DOptionPanel;
import com.mugui.Dui.DTextArea;
import com.mugui.base.base.Component;

import cn.hutool.core.util.StrUtil;
import cn.net.mugui.net.pc.manager.FunctionUI;

@Component
public class Xlxs2Panel extends FunctionUI {
	public Xlxs2Panel() {
		setTitle("数据回填");
		setMenu_name("数据回填");
		setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);

		DButton button = new DButton("一键摸鱼", (Color) null);
		button.setText("需要处理的文件");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String open = open(true);
				if (open == null)
					return;
				String[] split = open.split("<br>");
				in_files = new File[split.length];
				textArea.setText("");
				textArea.updateUI();
				textArea.validate();
				String str = "";
				for (int i = 0; i < split.length; i++) {
					in_files[i] = new File(split[i]);
					if (!in_files[i].exists()) {
						DOptionPanel.showMessageDialog(Xlxs2Panel.this, in_files[i].getAbsolutePath() + "不是一个文件",
								"error", DOptionPanel.OPTION_OK);
						return;
					}
					str += in_files[i] + "\r\n";

				}
				textArea.setText(str);
				textArea.updateUI();
				textArea.validate();

//				open = open(false);
//				if (open == null)
//					return;
//				out_file = new File(open);
//				if (!out_file.exists()) {
//					out_file = null;
//					return;
//				}
//				try {
//					handle();
//				} catch (Exception e1) {
//					e1.printStackTrace();
//				}
			}

		});
		panel.add(button);

		DButton button_1 = new DButton("一键摸鱼", (Color) null);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (in_files == null || in_files.length <= 0) {
					DOptionPanel.showMessageDialog(Xlxs2Panel.this, "未选择输入文件", "error", DOptionPanel.OPTION_OK);
					return;
				}

				String open = open(false);
				if (open == null)
					return;
				String[] split = open.split("<br>");
				out_file = new File(split[0]);
				if (!out_file.exists()) {
					DOptionPanel.showMessageDialog(Xlxs2Panel.this, out_file.getAbsolutePath() + "不是一个文件", "error",
							DOptionPanel.OPTION_OK);
					return;
				}

				int showMessageDialog = DOptionPanel.showMessageDialog(Xlxs2Panel.this, "点击确认开始", "ok",
						DOptionPanel.OPTION_OK_CANCEL);
				if (showMessageDialog == DOptionPanel.AGREE) {
					try {
						handle();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}

			}

		});
		button_1.setText("输出到目标文件");
		panel.add(button_1);

		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));

		JPanel panel_2 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		panel_1.add(panel_2, BorderLayout.NORTH);

		JLabel lblNewLabel = new JLabel("待处理的文件列表");
		panel_2.add(lblNewLabel);

		textArea = new DTextArea();
		textArea.setEditable(false);
		panel_1.add(textArea, BorderLayout.CENTER);

	}

	File[] in_files = null;
	File out_file = null;

	Workbook wb = null;
	Workbook write_file = null;
	Sheet cloneSheet = null;
	String str = null;

	int sheet_gongcheng_index = 0;
	int sheet_no_gongcheng_index = 0;

	private void handle() throws IOException {
		InputStream out_input = null;
		FileInputStream fileInputStream = null;
		try {
			String[] split1 = out_file.getName().split("[.]");
			out_input = new FileInputStream(out_file);
			if ("xls".equals(split1[split1.length - 1])) { // 文件流对象
				write_file = new HSSFWorkbook(out_input);
			} else if ("xlsx".equals(split1[split1.length - 1])) {
				write_file = new XSSFWorkbook(out_input);
			} else {
				DOptionPanel.showMessageDialog(Xlxs2Panel.this, "文件类型错误!", "error", DOptionPanel.OPTION_OK_CANCEL);
				return;
			}
			// 找到write_file 最后一行
			Sheet sheet_gongcheng = write_file.getSheet("导入工程建设领域");
			Sheet sheet_no_gongcheng = write_file.getSheet("导入非工程建设领域");
			sheet_gongcheng_index = 0;
			sheet_no_gongcheng_index = 0;
			sheet_gongcheng.forEach(new Consumer<Row>() {

				@Override
				public void accept(Row t) {
					Cell cell = t.getCell(0);
					CellType cellType = cell.getCellType();
					System.out.println(cellType.name());
					sheet_gongcheng_index++;
				}
			});
			sheet_no_gongcheng.forEach(new Consumer<Row>() {

				@Override
				public void accept(Row t) {
					Cell cell = t.getCell(0);
					CellType cellType = cell.getCellType();
					System.out.println(cellType.name());
					sheet_no_gongcheng_index++;
				}
			});

			for (File handle_file : in_files) {
				System.out.println(handle_file.getAbsolutePath());
				fileInputStream = new FileInputStream(handle_file);
				String[] split = handle_file.getName().split("[.]");
				if ("xls".equals(split[split.length - 1])) { // 文件流对象
					wb = new HSSFWorkbook(fileInputStream);
				} else if ("xlsx".equals(split[split.length - 1])) {
					wb = new XSSFWorkbook(fileInputStream);
				} else {
					DOptionPanel.showMessageDialog(Xlxs2Panel.this, "文件类型错误!", "error", DOptionPanel.OPTION_OK_CANCEL);
					return;
				}

				Sheet sheetAt = wb.getSheetAt(0);
				Cell cell = sheetAt.getRow(2).getCell(6);
				String stringCellValue = cell.getStringCellValue();
				if (stringCellValue.trim().equals("非工程建设领域")) {
					// 线索编号
					Cell cell2 = sheet_no_gongcheng.createRow(sheet_no_gongcheng_index).createCell(0);
					cell2.setCellValue(sheetAt.getRow(1).getCell(1).getStringCellValue());
					// 基本案情
					cell2 = sheet_no_gongcheng.getRow(sheet_no_gongcheng_index).createCell(1);
					cell2.setCellValue(sheetAt.getRow(8).getCell(1).getStringCellValue());

					// 核实处置情况
					cell2 = sheet_no_gongcheng.getRow(sheet_no_gongcheng_index).createCell('V' - 'A');
					cell2.setCellValue(sheetAt.getRow(13).getCell(1).getStringCellValue());

					// 承办单位
					cell2 = sheet_no_gongcheng.getRow(sheet_no_gongcheng_index).createCell('W' - 'A');
					cell2.setCellValue("锦江区根治办");
					// 必录
					cell2 = sheet_no_gongcheng.getRow(sheet_no_gongcheng_index).createCell('Y' - 'A');
					cell2.setCellValue("86612168");
					sheet_no_gongcheng_index++;
				} else {
					// 线索编号
					Cell cell2 = sheet_gongcheng.createRow(sheet_gongcheng_index).createCell(0);
					cell2.setCellValue(sheetAt.getRow(1).getCell(1).getStringCellValue());
					// 基本案情
					cell2 = sheet_gongcheng.getRow(sheet_gongcheng_index).createCell(1);
					cell2.setCellValue(sheetAt.getRow(8).getCell(1).getStringCellValue());

					// 核实处置情况
					cell2 = sheet_gongcheng.getRow(sheet_gongcheng_index).createCell(29);
					cell2.setCellValue(sheetAt.getRow(13).getCell(1).getStringCellValue());

					// 承办单位
					cell2 = sheet_gongcheng.getRow(sheet_gongcheng_index).createCell(30);
					cell2.setCellValue("锦江区根治办");
					// 必录
					cell2 = sheet_gongcheng.getRow(sheet_gongcheng_index).createCell(32);
					cell2.setCellValue("86612168");
					sheet_gongcheng_index++;
				}
				fileInputStream.close();

			}
			File file = new File(out_file.getParent() + "/" + out_file.getName().split("[.]")[0] + "_处理.xlsx");

			FileOutputStream out = new FileOutputStream(file);
			write_file.write(out);
			out.close();
			textArea.setText(textArea.getText() + "\r\n" + file.getAbsolutePath());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out_input != null) {
				out_input.close();
			}
			if (fileInputStream != null) {
				fileInputStream.close();
			}
		}
	}
//	private void handle() throws Exception {
//		String[] split = in_files.getName().split("[.]");
//		FileInputStream fis = null;
//		InputStream stream = null;
//		try {
//			fis = new FileInputStream(in_files);
//			stream = ResourceUtil.getStream("目标源1.0.xlsx");
//			write_file = new XSSFWorkbook(stream);
//			if ("xls".equals(split[split.length - 1])) { // 文件流对象
//				wb = new HSSFWorkbook(fis);
//			} else if ("xlsx".equals(split[split.length - 1])) {
//				wb = new XSSFWorkbook(fis);
//
//			} else {
//				System.out.println("文件类型错误!");
//				return;
//			}
//			Sheet sheetAt = wb.getSheetAt(0);
//			HashMap<Integer, String> title_map = new HashMap<>();
//
//			sheetAt.forEach(new Consumer<Row>() {
//
//				@Override
//				public void accept(Row row) {
//					if (row.getRowNum() > 0) {
////						Sheet sheet = write_file.getSheet("Sheet1");
//						Sheet sheet = write_file.getSheetAt(0);
//						cloneSheet = write_file.cloneSheet(write_file.getSheetIndex(sheet));
//						write_file.setSheetName(write_file.getSheetIndex(cloneSheet),
//								sheet.getSheetName() + (row.getRowNum()));
//
//					}
//					str = null;
//					row.iterator().forEachRemaining(new Consumer<Cell>() {
//						@Override
//						public void accept(Cell t) {
//							String stringCellValue = null;
//							try {
//								stringCellValue = t.getStringCellValue();
//							} catch (Exception e) {
//								try {
//									stringCellValue = new BigDecimal(t.getNumericCellValue()).toPlainString();
//								} catch (Exception e1) {
//									e1.printStackTrace();
//								}
//							}
//							if (row.getRowNum() == 0) {
//								title_map.put(t.getColumnIndex(), stringCellValue.trim());
//							} else {
//								String string = title_map.get(t.getColumnIndex());
//								System.out.println(string + "   ->  " + stringCellValue);
//								Cell cell = null;
//								switch (string) {
//								case "反映人姓名":
//									cell = cloneSheet.getRow(2).getCell(2);
//									break;
//								case "反映人电话":
//									cell = cloneSheet.getRow(3).getCell(2);
//									break;
//								case "反映人身份证号码":
//									cell = cloneSheet.getRow(3).getCell(4);
//									break;
//								case "行业领域":
//									cell = cloneSheet.getRow(2).getCell(6);
//									break;
//								case "拖欠工资项目（单位）名称":
//									cell = cloneSheet.getRow(5).getCell(2);
//									break;
//								case "拖欠工资单位电话":
//									cell = cloneSheet.getRow(6).getCell(4);
//									break;
//								case "是否签订劳动合同":
//									cell = cloneSheet.getRow(6).getCell(6);
//									break;
//								case "劳动合同用人单位名称":
//									cell = cloneSheet.getRow(5).getCell(2);
//									break;
//								case "实际用工所在地":
////										cell = cloneSheet.getRow(6).getCell(2);
//									break;
//								case "详细地址":
////										cell = cloneSheet.getRow(4).getCell(4);
//									cell = cloneSheet.getRow(7).getCell(2);
//									break;
//								case "欠薪人数（人）":
//									cell = cloneSheet.getRow(4).getCell(2);
//									break;
//								case "欠薪金额（元）":
//									cell = cloneSheet.getRow(4).getCell(4);
//									break;
//								case "欠薪发生时间":
//									cell = cloneSheet.getRow(4).getCell(6);
//									break;
//								case "线索编号":
//									cell = cloneSheet.getRow(1).getCell(1);
//									write_file.setSheetName(write_file.getSheetIndex(cloneSheet), stringCellValue);
//									break;
//								case "线索登记时间":
//									cell = cloneSheet.getRow(1).getCell(4);
//									break;
//								case "欠薪基本情况摘要":
//									cell = cloneSheet.getRow(8).getCell(1);
//									break;
//								case "是否曾经通过以下途径反映上述问题":
////										cell = cloneSheet.getRow(9).getCell(1);
//									str = "是否曾经通过以下途径反映上述问题\n";
//									str += stringCellValue + "\n\n";
//									break;
//								case "对曾经反映情况进行详细说明":
////										cell = cloneSheet.getRow(9).getCell(1);
//									str = "对曾经反映情况进行详细说明\n";
//									str += stringCellValue + "\n\n";
//									break;
//								}
//
//								if (cell != null) {
//									cell.setCellValue(stringCellValue);
//								}
//							}
//
//						}
//					});
//					if (str != null) {
//						Cell cell = cloneSheet.getRow(10).getCell(1);
//						cell.setCellValue(str);
//					}
//				}
//			});
//			write_file.removeSheetAt(write_file.getSheetIndex(write_file.getSheetAt(0)));
//			FileOutputStream out = new FileOutputStream(
//					new File(in_files.getParent() + "/" + in_files.getName().split("[.]")[0] + "_处理.xlsx"));
//			write_file.write(out);
//			out.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			if (stream != null) {
//				stream.close();
//			}
//			if (fis != null) {
//				fis.close();
//			}
//			if (write_file != null) {
//				write_file.close();
//			}
//			if (wb != null)
//				wb.close();
//		}
//	}
//	private void handle() throws Exception {
//		String[] split = select_file.getName().split("[.]");
//		FileInputStream fis = null;
//		InputStream stream = null;
//		try {
//			fis = new FileInputStream(select_file);
//			stream = ResourceUtil.getStream("欠薪线索登记表.xlsx");
//			write_file = new XSSFWorkbook(stream);
//			if ("xls".equals(split[split.length - 1])) { // 文件流对象
//				wb = new HSSFWorkbook(fis);
//			} else if ("xlsx".equals(split[split.length - 1])) {
//				wb = new XSSFWorkbook(fis);
//
//			} else {
//				System.out.println("文件类型错误!");
//				return;
//			}
//			Sheet sheetAt = wb.getSheetAt(0);
//			HashMap<Integer, String> title_map = new HashMap<>();
//
//			sheetAt.forEach(new Consumer<Row>() {
//
//				@Override
//				public void accept(Row row) {
//					if (row.getRowNum() > 0) {
//						Sheet sheet = write_file.getSheet("Sheet1");
//						cloneSheet = write_file.cloneSheet(write_file.getSheetIndex(sheet));
//						write_file.setSheetName(write_file.getSheetIndex(cloneSheet), "sheet" + (row.getRowNum() + 1));
//
//					}
//					str = null;
//					row.iterator().forEachRemaining(new Consumer<Cell>() {
//						@Override
//						public void accept(Cell t) {
//							String stringCellValue = null;
//							try {
//								stringCellValue = t.getStringCellValue();
//							} catch (Exception e) {
//								try {
//									stringCellValue = new BigDecimal(t.getNumericCellValue()).toPlainString();
//								} catch (Exception e1) {
//									e1.printStackTrace();
//								}
//							}
//							if (row.getRowNum() == 0) {
//								title_map.put(t.getColumnIndex(), stringCellValue.trim());
//							} else {
//								String string = title_map.get(t.getColumnIndex());
//								System.out.println(string + "   ->  " + stringCellValue);
//								Cell cell = null;
//								switch (string) {
//								case "反映人姓名":
//									cell = cloneSheet.getRow(1).getCell(2);
//									break;
//								case "反映人电话":
//									cell = cloneSheet.getRow(2).getCell(2);
//									break;
//								case "反映人身份证号码":
//									cell = cloneSheet.getRow(2).getCell(4);
//									break;
//								case "行业领域":
//									cell = cloneSheet.getRow(1).getCell(6);
//									break;
//								case "拖欠工资项目（单位）名称":
//									cell = cloneSheet.getRow(4).getCell(2);
//									break;
//								case "拖欠工资单位电话":
//									cell = cloneSheet.getRow(5).getCell(4);
//									break;
//								case "是否签订劳动合同":
//									cell = cloneSheet.getRow(5).getCell(6);
//									break;
//								case "劳动合同用人单位名称":
//									cell = cloneSheet.getRow(4).getCell(2);
//									break;
//								case "实际用工所在地":
////										cell = cloneSheet.getRow(6).getCell(2);
//									break;
//								case "详细地址":
////										cell = cloneSheet.getRow(4).getCell(4);
//									cell = cloneSheet.getRow(6).getCell(2);
//									break;
//								case "欠薪人数（人）":
//									cell = cloneSheet.getRow(3).getCell(2);
//									break;
//								case "欠薪金额（元）":
//									cell = cloneSheet.getRow(3).getCell(4);
//									break;
//								case "欠薪发生时间":
//									cell = cloneSheet.getRow(3).getCell(6);
//									break;
//								case "欠薪基本情况摘要":
//									cell = cloneSheet.getRow(7).getCell(1);
//									break;
//								case "是否曾经通过以下途径反映上述问题":
////										cell = cloneSheet.getRow(9).getCell(1);
//									str = "是否曾经通过以下途径反映上述问题\n";
//									str += stringCellValue + "\n\n";
//									break;
//								case "对曾经反映情况进行详细说明":
////										cell = cloneSheet.getRow(9).getCell(1);
//									str = "对曾经反映情况进行详细说明\n";
//									str += stringCellValue + "\n\n";
//									break;
//								}
//
//								if (cell != null) {
//									cell.setCellValue(stringCellValue);
//								}
//							}
//
//						}
//					});
//					if (str != null) {
//						Cell cell = cloneSheet.getRow(9).getCell(1);
//						cell.setCellValue(str);
//					}
//				}
//			});
//			write_file.removeSheetAt(write_file.getSheetIndex(write_file.getSheet("Sheet1")));
//			FileOutputStream out = new FileOutputStream(
//					new File(select_file.getParent() + "/" + select_file.getName().split("[.]")[0] + "_处理.xlsx"));
//			write_file.write(out);
//			out.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			if (stream != null) {
//				stream.close();
//			}
//			if (fis != null) {
//				fis.close();
//			}
//			if (write_file != null) {
//				write_file.close();
//			}
//			if (wb != null)
//				wb.close();
//		}
//	}

	@Override
	public void init() {
//		OPENFILENAME openfilename = new OPENFILENAME();
//
//		long /*int*/ hHeap = OS.GetProcessHeap ();
//		/* Use the character encoding for the default locale */
//		TCHAR buffer4 = new TCHAR (0, ".xls;.xlxs;.*", true);
//		int byteCount4 = buffer4.length () * TCHAR.sizeof;
//		long /*int*/ lpstrFilter = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount4);
//		OS.MoveMemory (lpstrFilter, buffer4, byteCount4);
//		openfilename.lpstrFilter=lpstrFilter;
//		OS.GetOpenFileNameW(openfilename);
//		System.out.println(openfilename);
//		open();
	}

	String[] filterNames = new String[0];
	String[] filterExtensions = new String[0];
	String[] fileNames = new String[0];
	String filterPath = "", fileName = "";
	int filterIndex = 0;
	boolean overwrite = false;
	static final String FILTER = "*.*";
	static int BUFFER_SIZE = 1024 * 32;
	static boolean USE_HOOK = true;
	private DTextArea textArea;
	static {
		/*
		 * Feature in Vista. When OFN_ENABLEHOOK is set in the save or open file dialog,
		 * Vista uses the old XP look and feel. OFN_ENABLEHOOK is used to grow the file
		 * name buffer in a multi-select file dialog. The fix is to only use
		 * OFN_ENABLEHOOK when the buffer has overrun.
		 */
		if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(6, 0)) {
			USE_HOOK = false;
		}
	}

	/**
	 * Makes the dialog visible and brings it to the front of the display.
	 *
	 * @return a string describing the absolute path of the first selected file, or
	 *         null if the dialog was cancelled or an error occurred
	 *
	 * @exception SWTException
	 *                         <ul>
	 *                         <li>ERROR_WIDGET_DISPOSED - if the dialog has been
	 *                         disposed</li>
	 *                         <li>ERROR_THREAD_INVALID_ACCESS - if not called from
	 *                         the thread that created the dialog</li>
	 *                         </ul>
	 */
	public String open(boolean is_多文件) {
		long /* int */ hHeap = OS.GetProcessHeap();

		/*
		 * Feature in Windows. There is no API to set the orientation of a file dialog.
		 * It is always inherited from the parent. The fix is to create a hidden parent
		 * and set the orientation in the hidden parent for the dialog to inherit.
		 */
		boolean enabled = false;

		String title;
		/* Convert the title and copy it into lpstrTitle */
		/* Use the character encoding for the default locale */
		TCHAR buffer3 = new TCHAR(0, "", true);
		int byteCount3 = buffer3.length() * TCHAR.sizeof;
		long /* int */ lpstrTitle = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount3);
		OS.MoveMemory(lpstrTitle, buffer3, byteCount3);

		/* Compute filters and copy into lpstrFilter */
		String strFilter = "";
		if (filterNames == null)
			filterNames = new String[0];
		if (filterExtensions == null)
			filterExtensions = new String[0];
		for (int i = 0; i < filterExtensions.length; i++) {
			String filterName = filterExtensions[i];
			if (i < filterNames.length)
				filterName = filterNames[i];
			strFilter = strFilter + filterName + '\0' + filterExtensions[i] + '\0';
		}
		if (filterExtensions.length == 0) {
			strFilter = strFilter + FILTER + '\0' + FILTER + '\0';
		}
		strFilter = "All Files(*.*)";
//		strFilter=".xls;.xlxs";
		/* Use the character encoding for the default locale */
		TCHAR buffer4 = new TCHAR(0, strFilter, true);
		int byteCount4 = buffer4.length() * TCHAR.sizeof;
		long /* int */ lpstrFilter = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount4);
		OS.MoveMemory(lpstrFilter, buffer4, byteCount4);

		/* Convert the fileName and filterName to C strings */
		if (fileName == null)
			fileName = "";
		/* Use the character encoding for the default locale */
		TCHAR name = new TCHAR(0, fileName, true);

		/*
		 * Copy the name into lpstrFile and ensure that the last byte is NULL and the
		 * buffer does not overrun.
		 */
		int nMaxFile = OS.MAX_PATH * 1024;

		int byteCount = nMaxFile * TCHAR.sizeof;
		long /* int */ lpstrFile = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
		int byteCountFile = Math.min(name.length() * TCHAR.sizeof, byteCount - TCHAR.sizeof);
		OS.MoveMemory(lpstrFile, name, byteCountFile);

		/*
		 * Copy the path into lpstrInitialDir and ensure that the last byte is NULL and
		 * the buffer does not overrun.
		 */
		if (filterPath == null)
			filterPath = "";
		/* Use the character encoding for the default locale */
		TCHAR path = new TCHAR(0, filterPath.replace('/', '\\'), true);
		int byteCount5 = OS.MAX_PATH * TCHAR.sizeof;
		long /* int */ lpstrInitialDir = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount5);
		int byteCountDir = Math.min(path.length() * TCHAR.sizeof, byteCount5 - TCHAR.sizeof);
		OS.MoveMemory(lpstrInitialDir, path, byteCountDir);

		/* Create the file dialog struct */
		OPENFILENAME struct = new OPENFILENAME();
		struct.lStructSize = OPENFILENAME.sizeof;
		if (!is_多文件) {
			struct.Flags = OS.OFN_HIDEREADONLY | OS.OFN_NOCHANGEDIR;
			struct.Flags |= OS.OFN_OVERWRITEPROMPT;
		} else {
			struct.Flags = OS.OFN_EXPLORER | OS.OFN_ALLOWMULTISELECT;
		}

		struct.lpstrTitle = lpstrTitle;
		struct.lpstrFile = lpstrFile;
		struct.nMaxFile = nMaxFile;
		struct.lpstrInitialDir = lpstrInitialDir;
		struct.lpstrFilter = lpstrFilter;
		struct.nFilterIndex = filterIndex == 0 ? filterIndex : filterIndex + 1;

		/*
		 * Set the default extension to an empty string. If the user fails to type an
		 * extension and this extension is empty, Windows uses the current value of the
		 * filter extension at the time that the dialog is closed.
		 */
		long /* int */ lpstrDefExt = 0;

		/*
		 * Open the dialog. If the open fails due to an invalid file name, use an empty
		 * file name and open it again.
		 */
		boolean sucess = OS.GetOpenFileName(struct);

		lpstrFile = struct.lpstrFile;

		/* Set the new path, file name and filter */
		fileNames = new String[0];
		String fullPath = null;
		if (sucess) {

			/* Use the character encoding for the default locale */
			TCHAR buffer = new TCHAR(0, struct.nMaxFile);
			int byteCount1 = buffer.length() * TCHAR.sizeof;
			OS.MoveMemory(buffer, lpstrFile, byteCount1);

			/*
			 * Bug in WinCE. For some reason, nFileOffset and nFileExtension are always zero
			 * on WinCE HPC. nFileOffset is always zero on WinCE PPC when using
			 * GetSaveFileName(). nFileOffset is correctly set on WinCE PPC when using
			 * OpenFileName(). The fix is to parse lpstrFile to calculate nFileOffset.
			 * 
			 * Note: WinCE does not support multi-select file dialogs.
			 */
			int nFileOffset = struct.nFileOffset;
			if (OS.IsWinCE && nFileOffset == 0) {
				int index = 0;
				while (index < buffer.length()) {
					int ch = buffer.tcharAt(index);
					if (ch == 0)
						break;
					if (ch == '\\')
						nFileOffset = index + 1;
					index++;
				}
			}
			if (nFileOffset > 0) {

				/* Use the character encoding for the default locale */
				TCHAR prefix = new TCHAR(0, nFileOffset - 1);
				int byteCount2 = prefix.length() * TCHAR.sizeof;
				OS.MoveMemory(prefix, lpstrFile, byteCount2);
				filterPath = prefix.toString(0, prefix.length());

				/*
				 * Get each file from the buffer. Files are delimited by a NULL character with 2
				 * NULL characters at the end.
				 */
				int count = 0;
				int start = nFileOffset;
				do {
					int end = start;
					while (end < buffer.length() && buffer.tcharAt(end) != 0)
						end++;
					String string = buffer.toString(start, end - start);
					start = end;
					if (count == fileNames.length) {
						String[] newFileNames = new String[fileNames.length + 4];
						System.arraycopy(fileNames, 0, newFileNames, 0, fileNames.length);
						fileNames = newFileNames;
					}
					fileNames[count++] = string;
					start++;
				} while (start < buffer.length() && buffer.tcharAt(start) != 0);

//				if (fileNames.length > 0)
//					fileName = fileNames[0];
				String separator = "";
				fullPath = "";
				for (String str : fileNames) {
					if (StrUtil.isNotBlank(str)) {
						fileName = str;
						int length = filterPath.length();
						if (length > 0 && filterPath.charAt(length - 1) != '\\') {
							separator = "\\";
						}
						fullPath += filterPath + separator + fileName + "<br>";
					}
				}

				if (count < fileNames.length) {
					String[] newFileNames = new String[count];
					System.arraycopy(fileNames, 0, newFileNames, 0, count);
					fileNames = newFileNames;
				}
			}
			filterIndex = struct.nFilterIndex - 1;

		}
		/* Free the memory that was allocated. */
		OS.HeapFree(hHeap, 0, lpstrFile);
		OS.HeapFree(hHeap, 0, lpstrFilter);
		OS.HeapFree(hHeap, 0, lpstrInitialDir);
		OS.HeapFree(hHeap, 0, lpstrTitle);
		if (lpstrDefExt != 0)
			OS.HeapFree(hHeap, 0, lpstrDefExt);
		return fullPath;
	}

	@Override
	public void quit() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dataInit() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dataSave() {
		// TODO Auto-generated method stub

	}

}
